package command

import (
	"strings"
	"time"

	"ssdevice/common/dto"
	"ssdevice/common/utils"
	"ssdevice/internal/rule/model"
)

type ProcessorInsertReq struct {
	Name     string `json:"name" vd:"len($)>0"`
	Script   string `json:"script"`
	Previous string `json:"previous" vd:"len($)>0"`
	Next     string `json:"next" vd:"len($)>0"`
	RuleId   string `json:"ruleId" vd:"len($)>0"`
	Loc      string `json:"loc"`
	Trigger  string `json:"trigger" vd:"len($)>0"`
	Schedule string `json:"schedule"`
	Desc     string `json:"desc"`
}

func (r *ProcessorInsertReq) Generate(model *model.Processor) error {
	model.Id = utils.SimpleUUID()
	model.Script = strings.TrimSpace(r.Script)
	model.RuleId = strings.TrimSpace(r.RuleId)
	model.Previous = strings.TrimSpace(r.Previous)
	model.Next = strings.TrimSpace(r.Next)
	model.Name = strings.TrimSpace(r.Name)
	model.Desc = strings.TrimSpace(r.Desc)
	model.Loc = strings.TrimSpace(r.Loc)
	model.Trigger = strings.TrimSpace(r.Trigger)
	model.Schedule = strings.TrimSpace(r.Schedule)
	now := dto.LocalTime(time.Now())
	model.CreateTime = now
	model.UpdateTime = now

	return nil
}

type ProcessorOperReq struct {
	Id string `json:"id" vd:"len($)>0"`
}

type ProcessorUpdateReq struct {
	Id       string `json:"id"`
	Name     string `json:"name"`
	Script   string `json:"script"`
	Previous string `json:"previous"`
	Next     string `json:"next"`
	Trigger  string `json:"trigger"`
	Schedule string `json:"schedule"`
	Loc      string `json:"loc"`
	Desc     string `json:"desc"`
}

func (r *ProcessorUpdateReq) Generate(model *model.Processor) error {
	model.Id = strings.TrimSpace(r.Id)
	model.Name = strings.TrimSpace(r.Name)
	model.Script = strings.TrimSpace(r.Script)
	model.Previous = strings.TrimSpace(r.Previous)
	model.Next = strings.TrimSpace(r.Next)
	model.Desc = strings.TrimSpace(r.Desc)
	model.Loc = strings.TrimSpace(r.Loc)
	model.Trigger = strings.TrimSpace(r.Trigger)
	model.Schedule = strings.TrimSpace(r.Schedule)
	now := dto.LocalTime(time.Now())
	model.UpdateTime = now

	return nil
}
